package com.lian.dao.impl;

import com.lian.dao.productdao;
import com.lian.entity.product;
import com.lian.util.DataBase;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by lenovo on 2018/8/6.
 */
public class productdaoimpl implements productdao
{
    @Override
    public List<product> findpro(int proid) {
        List<product>list=new ArrayList<product>();
        Connection conn= DataBase.getConnection();
        PreparedStatement stat=null;
        ResultSet rs=null;
        String sql="select*from product where wpid=?";
        try {
            stat = conn.prepareStatement(sql);
            stat.setInt(1, proid);
            rs = stat.executeQuery();
            while(rs.next()){
            product pr=new product();
            pr.setWpid(rs.getInt("wpid"));
            pr.setWaterName(rs.getString("waterName"));
            list.add(pr);

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }finally{
            DataBase.closeConnection(conn, stat, rs);
        }
        return list;
    }
    //查询净水器名字 新增加载处理数据
    public List<product> findpro() {
        List<product>list=new ArrayList<product>();
        Connection conn= DataBase.getConnection();
        PreparedStatement stat=null;
        ResultSet rs=null;
        String sql="select*from product ";
        try {
            stat = conn.prepareStatement(sql);

            rs = stat.executeQuery();
            while(rs.next()){
                product pr=new product();
                pr.setWpid(rs.getInt("wpid"));
                pr.setWaterName(rs.getString("waterName"));
                list.add(pr);

            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }finally{
            DataBase.closeConnection(conn, stat, rs);
        }
        return list;
    }

    @Override
    public product findBy(int wpid) {
        product pr=new product();
        Connection conn= DataBase.getConnection();
        PreparedStatement stat=null;
        ResultSet rs=null;
        String sql="select * from product where wpid=?";
        try {
            stat = conn.prepareStatement(sql);
            stat.setInt(1, wpid);
            rs = stat.executeQuery();
            while(rs.next()){
                pr.setWpid(rs.getInt("wpid"));
                pr.setWaterName(rs.getString("waterName"));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }finally{
            DataBase.closeConnection(conn, stat, rs);
        }
        return pr;
    }
}
